package com.lym.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lym.reggie.common.BaseContext;
import com.lym.reggie.common.R;
import com.lym.reggie.entity.Dish;
import com.lym.reggie.entity.DishDto;
import com.lym.reggie.entity.ShoppingCart;
import com.lym.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    //查看购物车 获取购物车内商品的集合
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //。。。where userId = #{userId}
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        Long userId = BaseContext.getCurrentId();
        queryWrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);
        if (shoppingCarts!=null){
            return R.success(shoppingCarts);
        }
        return R.error("购物车无菜品");
    }

    //添加至购物车
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
        //若购物车中已有同类商品，则number+1；若无则新增
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        if (dishId!=null){
            //添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //添加的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
        }
        // 根据userId查询当前的购物车是否已有同样商品
        //select count from ShoppingCart where userId= and dishId=/setmealId=
        queryWrapper.eq(userId!=null,ShoppingCart::getUserId,userId);

        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        if (cartServiceOne!=null){
            //已有
            cartServiceOne.setNumber(cartServiceOne.getNumber()+1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            shoppingCartService.save(shoppingCart);
        }

        return R.success("添加成功");
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId!=null,ShoppingCart::getUserId,userId);

        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功！");
    }

    //减少购物车菜品或套餐
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //当此时number==1时，直接删除记录，否则移除此记录
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
        if (dishId!=null){//删除的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
        }

        //得到number select * from ShoppingCart where userId= and ...Id=
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        Integer number = one.getNumber();
        if (number>1){
            one.setNumber(number-1);
            shoppingCartService.updateById(one);
        }else if (number==1){
            shoppingCartService.remove(queryWrapper);
        }else {
            return R.error("操作异常！");
        }

        return R.success(one);
    }

}
